"""
Main LogAnalyzer program

Analyses firewall logs and provides statistics on blocked packets.

"""

import argparse
import sys
from support import packet,geoip,packetStats,output
import commands



def main():
	"""Main Function for Log Analyzer"""
	parser = argparse.ArgumentParser(description='LPD Frewall Log Analyser')
	parser.add_argument("logFile",help="Log File Location")
	parser.add_argument("-t","--type",help="Firewall Log Type",choices=['ufw','iptables'])
	parser.add_argument("-p","--pdf",help="outputs to PDF",default = '' , nargs=1,metavar=('PDF File Location'))

	args = parser.parse_args()
	file = args.logFile
	type = args.type.lower()

	try:
		fo = open(file, "r")
		lines = fo.readlines()
		fo.close()
	except:
		print "File Not Found: " + file
		sys.exit()

	packetList = []
	for x in lines:
		y = packet.packet()
		if type == 'ufw':
			y.createFromUFW(x)
		elif type == 'iptables':
			y.createFromIptables(x)
		y.getLocations()
		packetList.append(y)

	
	stats = packetStats.packetStatistics(packetList)
	
	
	if args.pdf == '':
		output.outputToCMD(stats)
	else:
		output.outputToPDF(stats,args.pdf[0])
	
	
		
if  __name__ =='__main__':main()